package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.Visitor;
import com.tanhua.dubbo.api.VisitorApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VisitorApiImpl implements VisitorApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitor> queryByUid(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("date")));
        query.limit(5);
        List<Visitor> list = mongoTemplate.find(query, Visitor.class);
        return list;
    }

    @Override
    public List<Visitor> queryByUid(Long userId, Long lastTime) {
        Query query = new Query(Criteria.where("userId").is(userId).and("date").gt(lastTime));
        query.with(Sort.by(Sort.Order.desc("date")));
        List<Visitor> list = mongoTemplate.find(query, Visitor.class);
        return list;
    }
}
